// page/order/order.js
var utils = require('../../utils/util.js');
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
Page({
	data: {
		tabs: ["全部", "待付款", "待发货", "待收货", "待评价"],
		activeIndex: 0,
		sliderOffset: 0,
		allOrder: [],               //全部订单
		waitPayment: [],       //待付款
		waitDistribution: [],  //待发货
		waitReceiving: [],     //待收货
		waitEvaluation: [],    //待评价
		width: ""
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		var that = this;
		var uid = wx.getStorageSync('uid');
		utils.GET({
			url: 'Order/order',
			params: {
				uid: 1
			},
			success: function (res) {
				var arr = res.data.d
				if (arr) {
					that.fenlei(arr)
				}
			},
		})

	},


	tabClick: function (e) {
		this.setData({
			sliderOffset: e.currentTarget.offsetLeft,
			activeIndex: e.currentTarget.id
		});
	},
	onShow: function () {
		var that = this
		wx.getStorage({
			key: 'orderTab',
			success: function (res) {
				if (!res.data) {
					that.setData({
						activeIndex: 0
					})
				} else {
					that.setData({
						activeIndex: res.data,
						sliderOffset: that.data.width / that.data.tabs.length * res.data
					})
				}
			},
		})
	},
	qxdd: function (e) {
		var that = this;
		var i = e.currentTarget.dataset.id
		var order_sn = e.currentTarget.dataset.ordersn
		var arr = that.data.allOrder
		for (var i = 0; i <= arr.length - 1; i++) {
			if (arr[i]['orderNo'] == order_sn) {
				arr.splice(i, 1)
				utils.GET({
					url: 'Order/delorder',
					params: {
						order_sn: order_sn 
					},
					success: function (res) {
						wx.showToast({
							title: '取消订单成功',
							icon: 'success',
							duration: 2000
						})

						that.fenlei(arr)
					},
				})
				break
			}
		}



	},
	fenlei: function (arr) {
		var that = this;
		// console.log(arr)
		var waitPayment = []
		var waitDistribution = []
		var waitReceiving = []
		var waitEvaluation = []

		console.log(arr)
		for (var i = 0; i <= arr.length - 1; i++) {
			if (arr[i]['status'] == 10) {
				arr[i]['status1'] = '待付款'
				waitPayment.push(arr[i])
			}
			else if (arr[i]['status'] == 20) {
				arr[i]['status1'] = '等待卖家发货'
				waitDistribution.push(arr[i])
			}
			else if (arr[i]['status'] == 30) {
				arr[i]['status1'] = '卖家已发货'
				waitReceiving.push(arr[i])
			}
			else if (arr[i]['status'] == 40) {
				arr[i]['status1'] = '待评价'
				waitEvaluation.push(arr[i])

			} else {
				arr[i]['status1'] = '已完成'
			}
		}
		wx.getSystemInfo({
			success: function (res1) {
				that.setData({
					allOrder: arr,
					waitPayment: waitPayment,
					waitDistribution: waitDistribution,
					waitReceiving: waitReceiving,
					waitEvaluation: waitEvaluation,

					sliderLeft: (res1.windowWidth / that.data.tabs.length - sliderWidth) / 2,
					sliderOffset: res1.windowWidth / that.data.tabs.length * that.data.activeIndex,
					width: res1.windowWidth,
				})
				wx.getStorage({
					key: 'orderTab',
					success: function (res) {
						if (!res.data) {
							that.setData({
								activeIndex: 0,

							})
						} else {
							that.setData({
								activeIndex: res.data,
								sliderOffset: that.data.width / that.data.tabs.length * res.data,

							})
						}
					},
				})
			}
		});


	},
	qrsh: function (e) {
		var that = this;
		var order_sn = e.currentTarget.dataset.ordersn
		var arr = that.data.allOrder
		wx.showModal({
			title: '提示',
			content: '确认收货吗？',
			success: function (res) {
				if (res.confirm) {
					for (var i = 0; i <= arr.length - 1; i++) {
						if (arr[i]['orderNo'] == order_sn) {
							arr[i]['status'] = 40
							console.log(arr[i])
							utils.GET({
								url: 'Order/daoorder',
								params: {
									order_sn: order_sn
								},
								success: function (res) {
									wx.showToast({
										title: '确认收货成功',
										icon: 'success',
										duration: 2000
									})
									wx.navigateTo({
										url: 'comment'
									})
								},
							})
							break
						}
					}
					that.fenlei(arr)
				} else if (res.cancel) {
					console.log('用户点击取消')
				}
			}
		})

	},
	ddfk: function (e) {
		var that = this;
		var order_sn = e.currentTarget.dataset.ordersn
		var price = e.currentTarget.dataset.orderprice
		var arr = that.data.allOrder
		var app = getApp()
		wx.setStorageSync('order_sn', order_sn) // 存储
		wx.setStorageSync('sum', price) // 存储
		wx.navigateTo({
			url: '/page/cart/payment'
		})
	}, orderinfo: function (e) {
		var order_sn = e.currentTarget.dataset.ordersn
		// console.log(order_sn)
		wx.navigateTo({
			url: 'orderdetail?sn=' + order_sn
		})
	}
})
